package com.zhenyi.liuxue.admin;

import java.util.List;

import com.jfinal.kit.JsonKit;
import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.liuxue.common.model.LxCnSchool;
import com.liuxue.common.model.LxMajorRankScore;
import com.liuxue.common.model.LxSchoolMajor;
import com.liuxue.common.model.LxSchoolRank;
import com.liuxue.common.model.LxUkSchool;

import cn.hutool.core.util.NumberUtil;

/**
 * 大学排名
 * 
 * @author cen
 *
 */
public class RankController extends AdminController {
	
	public static final String[] MAJOR_ARRAY = { "Arts & Humanities", "Education", "Law", "Psychology", "Business",
			"Clinical", "Computer Science", "Engineering", "Life Sciences", "Natural Science", "Social Sciences" };

	private int getIndexOfMajors(String name) {
		for (int i = 0; i < MAJOR_ARRAY.length; i++) {
			String item = MAJOR_ARRAY[i];
			if (name.toLowerCase().contains(item.toLowerCase())) {
				return i;
			}
		}
		return -1;
	}

	/**
	 * 英国大学对应中国的大学列表
	 */
	@Override
	public void list() {
		Long ukSchoolId = getParaToLong("ukSchoolId");
		if (ukSchoolId == null) {
			renderFailure(500, "uk shool id is null");
			return;
		}
		String keyword = getPara("keyword");
		LxUkSchool ukSchool = LxUkSchool.dao.findById(ukSchoolId);
		if (ukSchool == null) {
			renderFailure(404, "not found");
			return;
		}
		
		Kv kv = Kv.create();
		kv.set("ukSchoolId", ukSchoolId);
		if (StrKit.notBlank(keyword)) {
			kv.set("keyword", keyword);
			setAttr("keyword", keyword);
		}
		SqlPara sqlPara = db.getSqlPara("LXAdminFindSchoolRank", kv);
		Page<LxSchoolRank> pages = LxSchoolRank.dao.paginate(getPage(), getPageSize(), sqlPara);
		
		setRainbow(pages);
		setItems(JsonKit.toJson(pages.getList()));
		setAttr("ukSchoolId", ukSchoolId);
		setAttr("ukSchoolName", ukSchool.getName());
		setAttr("ukSchoolRemark", ukSchool.getRemark());
		setAttr("cnSchools", JsonKit.toJson(LxCnSchool.dao.findAll()));
		render("/lx/admin/ukSchools/rankList.html");
	}

	/**
	 * 更新中国大学对应英国大学排名
	 */
	public void updateRank() {
		Long cnSchoolId = getParaToLong("cnSchoolId");
		if (cnSchoolId == null) {
			renderError(404);
			return;
		}
		Long ukSchoolId = getParaToLong("ukSchoolId");
		if (ukSchoolId == null) {
			renderError(404);
			return;
		}
		Integer rank = getParaToInt("rank", 999);
		if (rank == null) {
			renderError(404);
			return;
		}
		LxSchoolRank schoolRank = LxSchoolRank.dao.findById(cnSchoolId, ukSchoolId);
		if (schoolRank == null) {
			schoolRank = new LxSchoolRank();
			schoolRank.setCnSchoolId(cnSchoolId);
			schoolRank.setUkSchoolId(ukSchoolId);
			schoolRank.save();
		}
		schoolRank.setRank(rank);
		schoolRank.update();
		renderSuccess();
	}
	
	/**
	 * 删除英国-中国大学排名
	 */
	public void deleteRank() {
		Long cnSchoolId = getParaToLong("cnSchoolId");
		Long ukSchoolId = getParaToLong("ukSchoolId");
		if (cnSchoolId == null || ukSchoolId == null) {
			renderFailure(500, "cn/uk school id is needed");
			return;
		}
		LxSchoolRank schoolRank = LxSchoolRank.dao.findById(cnSchoolId, ukSchoolId);
		renderFlag(schoolRank.delete());
	}

	/**
	 * 中国大学列表
	 */
	public void cnSchools() {
		String name = getPara("name");
		Kv kv = Kv.create();
		if (StrKit.notBlank(name)) {
			kv.set("name", "%" + name + "%");
		}
		SqlPara sqlPara = db.getSqlPara("LXFindAllCnSchool", kv);
		Page<Record> pages = db.paginate(getPage(), PAGE_SIZE, sqlPara);
		setData("items", pages.getList());
		setRainbow(pages);
		render("");
	}

	/**
	 * 更新中国大学信息
	 */
	public void updateCnSchool() {
		Long cnSchoolId = getParaToLong("cnSchoolId");
		String name = getPara("name");
		if (StrKit.isBlank(name)) {
			renderMsg("请输入大学名称");
			return;
		}
		LxCnSchool cnSchool = LxCnSchool.dao.findById(cnSchoolId);
		if (cnSchool == null) {
			cnSchool = new LxCnSchool();
			// Todo
			// 添加学校之后,每个英国大学都要添加排名
		}
		cnSchool.setName(name);
		cnSchool.update();
		renderSuccess();
	}

	/**
	 * 删除中国大学
	 */
	public void deleteCnSchool() {
		Long cnSchoolId = getParaToLong("cnSchoolId");
		if (cnSchoolId == null) {
			renderError(404);
			return;
		}
		LxCnSchool cnSchool = LxCnSchool.dao.findById(cnSchoolId);
		if (cnSchool == null) {
			renderError(404);
			return;
		}
		cnSchool.delete();
		// Todo:
		// 外键清除
		renderSuccess();
	}

	/**
	 * 英国大学,每个专业的排名要求
	 */
	public void rankScore() {
		Long ukSchoolId = getParaToLong("ukSchoolId");
		String keyword = getPara("keyword");
		Integer majorType = getParaToInt("majorType");
		if (ukSchoolId == null) {
			renderError(404);
			return;
		}
		LxUkSchool ukSchool = LxUkSchool.dao.findById(ukSchoolId);
		if (ukSchool == null) {
			renderFailure(404, "not found");
			return;
		}
		Kv kv = Kv.create();
		kv.set("ukSchoolId", ukSchoolId);
		if (StrKit.notBlank(keyword)) {
			kv.set("keyword", keyword);
			setAttr("keyword", keyword);
		}
		if (majorType != null) {
			kv.set("majorType", majorType);
			setAttr("majorType", majorType);
		}
		SqlPara sqlPara = db.getSqlPara("LXAdminFindMajorRank", kv);
		Page<Record> pages = db.paginate(getPage(), getPageSize(), sqlPara);
		
		for (Record record : pages.getList()) {
			if (StrKit.notBlank(record.getStr("requirement"))) {
				record.set("requirement", record.getStr("requirement").replace("<br />", "\n"));
			}
		}
		
		setRainbow(pages);
		setItems(JsonKit.toJson(pages.getList()));
		setAttr("ukSchoolId", ukSchoolId);
		setAttr("ukSchoolName", ukSchool.getName());
		setAttr("ukSchoolRemark", ukSchool.getRemark());
		render("/lx/admin/ukSchools/rankScoreList.html");
	}
	
	/**
	 * 录取标准详情
	 */
	public void rankScoreInfo() {
		setReferer();
		Long rankScoreId = getParaToLong("rankScoreId");
		Long ukSchoolId = getParaToLong("ukSchoolId");
		if (ukSchoolId == null) {
			renderFailure(500, "uk school id is null");
			return;
		}
		if (rankScoreId != null) {
			LxMajorRankScore info = LxMajorRankScore.dao.findById(rankScoreId);
			if (info == null) {
				renderFailure(404, "not found");
				return;
			}
			setInfoItem(JsonKit.toJson(info));
		}
		
		SqlPara sp1 = db.getSqlPara("LXAdminFindSchoolMajorForList", Kv.by("ukSchoolId", ukSchoolId));
		List<Record> schoolMajors = db.find(sp1);
		for (Record record : schoolMajors) {
			if (record.getInt("isNoMore") != null && record.getInt("isNoMore").equals(1)) {
				record.set("name", record.getStr("department"));
			}
		}
		setAttr("schoolMajors", JsonKit.toJson(schoolMajors));
		
		SqlPara sp2 = db.getSqlPara("LXAdminFindMajosForList", Kv.by("ukSchoolId", ukSchoolId));
		List<Record> majors = db.find(sp2);
		setAttr("majors", JsonKit.toJson(majors));
		setAttr("ukSchoolId", ukSchoolId);
		render("/lx/admin/ukSchools/rankScoreInfo.html");
	}

	/**
	 * 添加/修改排名
	 */
	public void updateRankScore() {
		Long majorRankScoreId = getParaToLong("majorRankScoreId");
		Long ukSchoolId = getParaToLong("ukSchoolId");
		Boolean isNewMajor = getParaToInt("isNewMajor", 0) == 1;
		Long majorId = getParaToLong("majorId");
		Integer minRank = getParaToInt("minRank", 1);
		Integer maxRank = getParaToInt("maxRank", 999);
		String score = getPara("score");
		String requirement = getPara("requirement");
		
		// 是否新增 LxSchoolMajor
		LxSchoolMajor schoolMajor = null;
		if (isNewMajor) {
			Integer isNoMore = getParaToInt("majorIsNoMore");
			String majorName = getPara("majorName");
			String majorDepartment = getPara("majorDepartment");
			Integer majorType = getParaToInt("majorType");
			
			schoolMajor = new LxSchoolMajor();
			schoolMajor.setSchoolId(ukSchoolId);
			schoolMajor.setName(isNoMore == 1 ? "null" : majorName);
			schoolMajor.setIntro(isNoMore == 1 ? "null" : majorName);
			schoolMajor.setDepartment(majorDepartment);
			schoolMajor.setMinRank(maxRank); // 最低排名要求
			schoolMajor.setScore(100);
			schoolMajor.setType(majorType);
			schoolMajor.setIsNoMore(isNoMore);
			schoolMajor.save();
		} else {
			if (majorId == null) {
				renderFailure(500, "major id is null");
				return;
			}
			schoolMajor = LxSchoolMajor.dao.findById(majorId);
			if (schoolMajor == null) {
				renderFailure(404, "major not found");
				return;
			}
		}
		
		LxMajorRankScore majorRankScore = null;
		if (majorRankScoreId == null) {
			majorRankScore = new LxMajorRankScore();
			majorRankScore.save();
		} else {
			majorRankScore = LxMajorRankScore.dao.findById(majorRankScoreId);
			if (majorRankScore == null) {
				renderFailure(404, "not found");
				return;
			}
		}
		majorRankScore.setMaxRank(maxRank);
		majorRankScore.setMinRank(minRank);
		if (schoolMajor.getMinRank() < maxRank) {
			schoolMajor.setMinRank(maxRank);
			schoolMajor.update();
		}
		if (NumberUtil.isNumber(score)) {
			majorRankScore.setScore(Integer.parseInt(score));
			
			if (schoolMajor.getScore() > Integer.parseInt(score)) {
				schoolMajor.setScore(Integer.parseInt(score));
				schoolMajor.update();
			}
			majorRankScore.setExtra("");
		} else {
			majorRankScore.setScore(100);
			majorRankScore.setExtra(score);
		}
		majorRankScore.setMajorId(schoolMajor.getId());
		majorRankScore.setRequirement(requirement);
		majorRankScore.update();
		renderSuccess();
	}

	/**
	 * 移除排名
	 */
	public void deleteRankScore() {
		Long majorRankScoreId = getParaToLong("majorRankScoreId");
		if (majorRankScoreId == null) {
			renderError(404);
			return;
		}
		LxMajorRankScore majorRankScore = LxMajorRankScore.dao.findById(majorRankScoreId);
		if (majorRankScore == null) {
			return;
		}
		majorRankScore.delete();
		renderSuccess();
	}

}
